package summerbug.hanzi.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;

import summerbug.hanzi.MainActivity;
import summerbug.hanzi.service.NetworkService;
import summerbug.hanzi.R;
import summerbug.hanzi.ZiDianActivity;

/**
 * Created by 76336 on 2018/5/12.
 */

public abstract class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener  {

    private Context mContext;
    private String string;
    private Toolbar toolbar;
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private ActionBarDrawerToggle mDrawerToggle;
    private SearchView mSearchView;

    private Intent netIntent;
    private Intent intent;
    private Intent intent2;
    private Bundle bundle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        init();
        initintent();
        initView();
        initToolbar();
    }

    private void initintent(){
        netIntent = new Intent(this, NetworkService.class);
        intent = new Intent(this,MainActivity.class);
        intent2 = new Intent(this,ZiDianActivity.class);
        bundle = new Bundle();

    };

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_toolbar,menu);
        MenuItem searchItem = menu.findItem(R.id.sv);
        mSearchView = (SearchView) searchItem.getActionView();

        mSearchView.setQueryHint("输入要查询的字");

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Log.d("tag", "onQueryTextSubmit: "+query);
                bundle.putString("zd",query);
                mSearchView.setQuery("",false);
                mSearchView.onActionViewCollapsed();
                intent2.putExtras(bundle);
                netIntent.putExtras(bundle);
                startService(netIntent);
                startActivity(intent2);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //Toast.makeText(mContext, newText, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        return true;
    }

    private void init() {
        string = "首页";
        toolbar = findViewById(R.id.toolbar);
        mDrawerLayout = findViewById(R.id.dl);
        mNavigationView = findViewById(R.id.nav_view);
    }

    protected void initView() {
    }

    private void initToolbar() {
        toolbar.setTitle(string);
        setSupportActionBar(toolbar);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,mDrawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close
        );

        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

        mNavigationView.setNavigationItemSelectedListener(this);
        mNavigationView.setFitsSystemWindows(true);
    }

    protected abstract int getLayout();

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        Log.d("tag"," "+id);
        switch (id){
            case R.id.nav_a:
                //字典
                startActivity(intent);
                Log.d("tag"," "+id);
                break;
            case R.id.nav_b:
                //生词本
                Log.d("tag"," "+id);
                break;
            case R.id.e:
                //设置
                Log.d("tag"," "+id);
                break;
        }
        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    public void setString(String string) {
        this.string = string;
    }

    public DrawerLayout getmDrawerLayout() {
        return mDrawerLayout;
    }


}
